package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.CommentSign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ICommentSignService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/commentSigns")
public class CommentSignController {

    @Autowired
    private ICommentSignService commentSignService;

    /**
     * 新增标记
     *
     * @param commentSign
     * @return
     */
    @PostMapping
    public JsonResult save(CommentSign commentSign) {
        JsonResult result = new JsonResult();
        //设置点赞或者收藏的时间
        commentSign.setUser(UserContext.getCurrentUser());
        try {
            commentSignService.save(commentSign);
            HashMap<Object, Object> map = new HashMap<>();
            int greatStatus = commentSignService.queryGreatStatusCountByCommentId(commentSign.getComment().getId());
            int enshrineStatus = commentSignService.queryEnshrineStatusCountByCommentId(commentSign.getComment().getId());
            map.put("greatStatus", greatStatus);
            map.put("enshrineStatus", enshrineStatus);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 更新标记
     *
     * @param commentSign
     * @return
     */
    @PutMapping("{id}")
    public JsonResult update(CommentSign commentSign) {
        JsonResult result = new JsonResult();
        commentSign.setUser(UserContext.getCurrentUser());
        try {
            commentSignService.update(commentSign);
            HashMap<Object, Object> map = new HashMap<>();
            int greatStatus = commentSignService.queryGreatStatusCountByCommentId(commentSign.getComment().getId());
            int enshrineStatus = commentSignService.queryEnshrineStatusCountByCommentId(commentSign.getComment().getId());
            map.put("greatStatus", greatStatus);
            map.put("enshrineStatus", enshrineStatus);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询当前用户在点评下的标记状态
     *
     * @param commentId
     * @return
     */
    @GetMapping("{commentId}")
    public CommentSign selectByCommentId(@PathVariable Long commentId) {
        User currentUser = UserContext.getCurrentUser();

        Long id = currentUser.getId();
        return commentSignService.selectByCommentId(commentId, UserContext.getCurrentUser().getId());
    }

    /**
     * 查询当前某个攻略的点赞和收藏总数
     *
     * @param commentId
     * @return
     */
    @GetMapping("count/{commentId}")
    public Map queryGreatStatusCountByCommentId(@PathVariable Long commentId) {
        HashMap<Object, Object> map = new HashMap<>();
        int greatStatus = commentSignService.queryGreatStatusCountByCommentId(commentId);
        int enshrineStatus = commentSignService.queryEnshrineStatusCountByCommentId(commentId);
        map.put("greatStatus", greatStatus);
        map.put("enshrineStatus", enshrineStatus);
        return map;
    }


}
